﻿using System;
using FunctionalCLR.Core;

namespace FunctionalCLR.Shell.MathematicaInterface {
    public class MMConvertNode : FuncNode {

        public MMConvertNode()
            : base() {
            base.Value = "mmConvert";
        }

        public override object Evaluate() {
            if (Parameters.Count < 1) {
                throw new ArgumentException(
                    "usage: mmConvert(expr);");
            }

            string expr = Convert.ToString(
               Parameters[0].Evaluate());
            Parser parser
                = DefaultDependencyManager.Instance.ParserFactory.CreateParser(expr);
            Node root = parser.Parse();
            MathematicaExpressionVisitor visitor 
                = new MathematicaExpressionVisitor("", root);
            string result = visitor.ConvertExpression();

            return result;
        }
    }
}
